var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');



// session
// =======
// >>>>>>> 37b78aa1168ab50108ee014713debbe21f346ec2
const urlModule = require("url");
// =======

// >>>>>>> a0a2f0b29f86a3c12ec7ce539353068cce17158b

const session = require("express-session");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// <<<<<<< HEAD
// <<<<<<< HEAD
//var usersglRouter = require('./routes/usersgl');
// =======
var countsRouter = require('./routes/counts');
// >>>>>>> a0a2f0b29f86a3c12ec7ce539353068cce17158b
// =======
// <<<<<<< HEAD
// <<<<<<< HEAD
var providersRouter = require('./routes/providers');
// =======
//var positionsRouter = require('./routes/positions');
// >>>>>>> 602f38b9f7e93fd6e8a53cb2f209dc372086273f
// =======
// var providersRouter = require('./routes/providers');
// >>>>>>> 79c9b497f3c65895301f80ba782aa12597a0c8a1
// >>>>>>> 37b78aa1168ab50108ee014713debbe21f346ec2

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// <<<<<<< HEAD
// app.use(logger('dev')); 
// =======
app.use(logger('dev'));
// >>>>>>> 79c9b497f3c65895301f80ba782aa12597a0c8a1
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

// <<<<<<< HEAD
// // <<<<<<< HEAD
// =======
// <<<<<<< HEAD
// <<<<<<< HEAD
// =======
// >>>>>>> 79c9b497f3c65895301f80ba782aa12597a0c8a1
// 使用 express-session 中间件，使得 Express 应用支持 session 处理
app.use(session({
  secret: 'asdfkjasidfjwoeqijrlksadufoashdf',
  resave: false,
  saveUninitialized: true,
  cookie: { maxAge: 30 * 60 * 1000 }
}));

// <<<<<<< HEAD
// =======
// >>>>>>> 37b78aa1168ab50108ee014713debbe21f346ec2
//使用express-session中间件，使得Express应用支持session处理
// app.use(session({
//   secret:'asdfghjklzxcvbnmqwertyuiop',
//   resave:false,
//   saveUninitialized:true,
//   cookie:{ maxAge: 30 * 60 * 1000 }
// }));

// 简单权限认证
app.use(function(req,res,next) {
  const {url} = req;
  const URL = urlModule.parse(url);
  // 获取访问的路径名称
  const pathname = URL.pathname;
  //console.log(pathname);
  // 判断是否有访问和 "position" 相关的资源
  if(pathname.indexOf("position")!== -1) {
    // 判断用户是否登录，如果没有登录，则跳转到首页
    const user = req.session.loginUser;
    if(!user) {
      res.redirect("/html/login.html");
      return;
    }
  }
  next();
});
// <<<<<<< HEAD
// =======
// 使用 express-session 中间件，使得 Express 应用支持 session 处理
app.use(session({
  secret: 'asdfkjasidfjwoeqijrlksadufoashdf',
  resave: false,
  saveUninitialized: true,
  cookie: { maxAge: 30 * 60 * 1000 }
}));






app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter); // 访问 "/" 目录下的资源
// <<<<<<< HEAD
// <<<<<<< HEAD
// app.use('/api/users', usersRouter); // 访问"api/users"目录下的资源
//app.use('/api/usersgls', usersglRouter); // 访问"api/usersgls"目录下的资源
// =======
app.use('/api/users', usersRouter); // 访问 "/api/users"目录下的资源
app.use("/api/counts", countsRouter); // 访问 "/api/counts"目录下的资源
// >>>>>>> a0a2f0b29f86a3c12ec7ce539353068cce17158b
// =======
// <<<<<<< HEAD
// <<<<<<< HEAD
// app.use('/api/users', usersRouter); // 访问 "/api/users"目录下的资源
app.use("/api/providers", providersRouter); // 访问 "/api/positions"目录下的资源
// =======
// app.use('/api/users', usersRouter); // 访问"api/users"目录下的资源
//app.use('/api/positions', positionsRouter); // 访问"api/positions"目录下的资源
// >>>>>>> 602f38b9f7e93fd6e8a53cb2f209dc372086273f
// =======
// app.use('/api/users', usersRouter); // 访问 "/api/users"目录下的资源
// app.use("/api/providers", providersRouter); // 访问 "/api/positions"目录下的资源
// >>>>>>> 79c9b497f3c65895301f80ba782aa12597a0c8a1
// >>>>>>> 37b78aa1168ab50108ee014713debbe21f346ec2

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
